﻿using Infrastructure.Attribute;
using Infrastructure.Extensions;
using Microsoft.IdentityModel.Tokens;
using Mysqlx.Session;
using System.Linq;
using ZR.Model;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Model.System;
using ZR.Repository;
using ZR.Service.IService.ProductionControl;

namespace ZR.Service.Service.ProductionControl {
    /// <summary>
    /// 采集曲线数据Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ICksCollectedCurveDataService), ServiceLifetime = LifeTime.Transient)]
    public class CksCollectedCurveDataService : BaseService<CksCollectedCurveData>, ICksCollectedCurveDataService {
        /// <summary>
        /// 查询采集曲线数据列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<CksCollectedCurveDataDto> GetList(CksCollectedCurveDataQueryDto parm) {
            
            var response = Queryable()
                .LeftJoin<PlcConfig>((a, b) => a.PlcId == b.Id)
                .WhereIF(!string.IsNullOrEmpty(parm.PrimaryCode),(a,b) => a.PrimaryCode.Contains(parm.PrimaryCode))
                .WhereIF(parm.BeginTime.HasValue && parm.EndTime.HasValue, (a,b) => a.CreateTime >= parm.BeginTime.Value && a.CreateTime <= parm.EndTime.Value)
                .Select((a, b) => new CksCollectedCurveDataDto {
                    Id = a.Id,
                    PlcId = a.PlcId,
                    DbBlock = a.DbBlock,
                    PrimaryCode = a.PrimaryCode,
                    CurveData = a.CurveData,
                    CollectBatchCode = a.CollectBatchCode,
                    CreateTime = a.CreateTime,
                    IsDeleted = a.IsDeleted,
                    LineCode = b.ProductionLineCode,
                    StationCode = b.WorkstationCode,
                })
                .ToPage(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public CksCollectedCurveData GetInfo(int Id) {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加采集曲线数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public CksCollectedCurveData AddCksCollectedCurveData(CksCollectedCurveData model) {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改采集曲线数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateCksCollectedCurveData(CksCollectedCurveData model) {
            return Update(model, true);
        }

        public PagedInfo<CksCollectedCurveData> ExportList(CksCollectedCurveDataQueryDto parm) {

            var response = Queryable().LeftJoin<PlcConfig>((a, b) => a.PlcId == b.Id)
                .WhereIF(!string.IsNullOrEmpty(parm.PrimaryCode), (a, b) => a.PrimaryCode.Contains(parm.PrimaryCode))
                .WhereIF(parm.BeginTime.HasValue && parm.EndTime.HasValue, (a, b) => a.CreateTime >= parm.BeginTime.Value && a.CreateTime <= parm.EndTime.Value)
                .Select((a, b) => new CksCollectedCurveData() {
                }, true)
                .ToPage(parm);

            return response;
        }
    }
}